<!--
	Copyright (C) 2010 Ethan Gruber
	EADitor: http://code.google.com/p/eaditor/
	Apache License 2.0: http://code.google.com/p/eaditor/
	
	Delete selected EAD guide from eXist (or other interface).  This is linked from the file list page -->
<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms"
	xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:fr="http://orbeon.org/oxf/xml/form-runner" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xi="http://www.w3.org/2001/XInclude">
	<head>
		<title>EADitor: Publish</title>
		<link type="text/css" href="http://localhost:8080/cocoon/numishare/xforms.css"
			rel="stylesheet"/>
		<xforms:model>
			<xforms:instance id="id">
				<!-- Start with placeholders for three files -->
				<id xmlns=""/>
			</xforms:instance>

			<!-- load URLs from site_info.xml in eXist into form for Solr and CSS file -->
			<xforms:instance id="site-info">
				<site_info xmlns="">
					<url/>
					<solr_url/>
				</site_info>
			</xforms:instance>

			<!-- send to Solr -->
			<xforms:instance id="addIndex">
				<add xmlns=""/>
			</xforms:instance>

			<!-- delete from Solr -->
			<xforms:instance id="deleteId">
				<delete xmlns="">
					<id/>
				</delete>
			</xforms:instance>

			<!-- Solr optimize -->
			<xforms:instance id="optimizeIndex">
				<optimize/>
			</xforms:instance>

			<!-- Instance for Solr commit-->
			<xforms:instance id="sendCommit">
				<commit/>
			</xforms:instance>

			<xforms:instance id="datatype">
				<datatype/>
			</xforms:instance>

			<xforms:instance id="status">
				<status/>
			</xforms:instance>

			<xforms:instance id="to-publish">
				<verification xmlns="">Click OK to publish <title/>.</verification>
			</xforms:instance>

			<xforms:instance id="unpublish">
				<verification xmlns="">Click OK to unpublish <title/>.</verification>
			</xforms:instance>

			<!-- get id parameter and set that to be the file to create or delete -->
			<xforms:action ev:event="xforms-model-construct-done">
				<xforms:setvalue ref="instance('id')" value="xxforms:get-request-parameter('id')"/>
				<xforms:setvalue ref="instance('datatype')"
					value="xxforms:get-request-parameter('datatype')"/>
				<xforms:setvalue ref="instance('deleteId')/id"
					value="xxforms:get-request-parameter('id')"/>
			</xforms:action>

			<!-- publish or unpublish depending on mode attribute -->
			<xforms:action ev:event="xforms-ready">
				<xxforms:variable name="mode" select="xxforms:get-request-parameter('mode')"/>

				<xforms:action if="$mode = 'create'">
					<xforms:setvalue ref="instance('to-publish')/title"
						value="xxforms:get-request-parameter('title')"/>
				</xforms:action>
				<xforms:action if="$mode = 'delete'">
					<xforms:setvalue ref="instance('unpublish')/title"
						value="xxforms:get-request-parameter('title')"/>
					<xforms:toggle case="remove-publication-verification"
						ev:event="xforms-model-construct-done"/>
				</xforms:action>
			</xforms:action>

			<!-- load site info -->
			<xforms:submission id="load-site-info" serialization="none" method="get"
				action="/exist/rest/db/numishare/site_info.xml" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="site-info"/>

			<!-- access service to read in pre-transformed solr doc -->
			<xforms:submission id="to-solr" method="get" replace="instance" instance="addIndex"
				serialization="none"
				resource="{instance('site-info')/url}display/{instance('datatype')}/{instance('id')}.solr">
				<xforms:message ev:event="xforms-submit-error" level="modal">Error transforming EAD
					guide to Solr document.</xforms:message>
				<xforms:send ev:event="xforms-submit-done" submission="publish-submission"/>
			</xforms:submission>

			<!-- post instance to Solr -->
			<xforms:submission id="publish-submission"
				action="{instance('site-info')/solr_url}update" ref="instance('addIndex')"
				instance="addIndex" replace="instance" method="post">
				<xforms:send ev:event="xforms-submit-done" submission="submit-commit"/>
				<xforms:setvalue ev:event="xforms-submit-done" ref="instance('status')">Artifact
					published.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Data Failed to POST to
					Solr!</xforms:message>
			</xforms:submission>

			<!-- delete from Solr -->
			<xforms:submission id="delete-from-solr" action="{instance('site-info')/solr_url}update"
				ref="instance('deleteId')" instance="deleteId" replace="none" method="post">
				<xforms:send ev:event="xforms-submit-done" submission="submit-commit"/>
				<xforms:setvalue ev:event="xforms-submit-done" ref="instance('status')">Artifact
					unpublished.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Data Failed to POST to
					Solr!</xforms:message>
			</xforms:submission>

			<!-- send commit -->
			<xforms:submission id="submit-commit" action="{instance('site-info')/solr_url}update"
				ref="instance('sendCommit')" instance="sendCommit" replace="none" method="post">
				<xforms:toggle case="status-update" ev:event="xforms-submit-done"/>
				<xforms:send ev:event="xforms-submit-done" submission="optimize"/>
			</xforms:submission>

			<!-- send optimize -->
			<xforms:submission id="optimize" action="{instance('site-info')/solr_url}update"
				ref="instance('optimizeIndex')" instance="optimizeIndex" replace="none"
				method="post">
				<xforms:message ev:event="xforms-submit-error" level="modal">Failed to
					optimize!</xforms:message>
			</xforms:submission>

			<xforms:send ev:event="xforms-model-construct-done" submission="load-site-info"/>
		</xforms:model>
	</head>
	<body>
		<div class="form">
			<!-- publish or unpublish depending on mode attribute -->
			<xforms:switch>
				<xforms:case id="publication-verification">
					<p>
						<xforms:output ref="instance('to-publish')"/>
					</p>

					<p>
						<xforms:submit submission="to-solr">
							<xforms:label>OK</xforms:label>
						</xforms:submit>
					</p>
				</xforms:case>
				<xforms:case id="remove-publication-verification">
					<p>
						<xforms:output ref="instance('unpublish')"/>
					</p>

					<p>
						<xforms:submit submission="delete-from-solr">
							<xforms:label>OK</xforms:label>
						</xforms:submit>
					</p>
				</xforms:case>
				<xforms:case id="status-update">
					<xforms:group ref="instance('status')/text()">
						<div class="success">
							<xforms:output ref="instance('status')"/>
						</div>
					</xforms:group>
				</xforms:case>
			</xforms:switch>
		</div>
		<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector"
			xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
	</body>
</html>
